package com.youlai.boot.system.service;

import com.google.common.collect.Lists;
import com.youlai.boot.system.model.Goods;
import com.youlai.boot.system.model.GoodsType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsService {

    public static final List<Goods> dataList = Lists.newArrayListWithCapacity(5);

    public static final List<GoodsType> goodsTypeList = Lists.newArrayListWithCapacity(100);

    public List<Goods> selectAll() {
        init();
        return dataList;
    }

    public List<Goods> delete(Long id) {
        Goods goods = new Goods();
        goods.setId(id);
        dataList.remove(goods);
        return dataList;
    }


    public int update(Goods goods) {
        Long id = goods.getId();
        if (Objects.nonNull(id)) {
            List<Goods> collect = dataList.stream().filter(item -> Objects.equals(id, item.getId())).collect(Collectors.toList());
            Goods dbModel = CollectionUtils.firstElement(collect);
            if (Objects.nonNull(dbModel)) {
                dbModel.setName(goods.getName());
                dbModel.setCode(goods.getCode());
                dbModel.setPicture(goods.getPicture());
                return 1;
            }
        }
        return 0;
    }

    public int save(Goods param) {
        Goods goods = new Goods();
        goods.setId((long) (dataList.size() + 1));
        goods.setName(param.getName());
        goods.setCode(param.getCode());
        goods.setPicture(param.getPicture());
        goods.setPicture(goodsPic());
        goods.setGoodsTypeId(param.getGoodsTypeId());
        dataList.add(goods);
        return 1;
    }


    public Goods selectById(Long id) {
        List<Goods> collect = dataList.stream().filter(item -> Objects.equals(id, item.getId())).collect(Collectors.toList());
        return CollectionUtils.firstElement(collect);
    }

    public void init() {
        if (CollectionUtils.isEmpty(dataList)) {
            Goods goods1 = new Goods(uuid());
            goods1.setId(1L);
            goods1.setGoodsTypeId(goodsTypeId());
            goods1.setName("马桶坐垫秋冬加厚家用毛绒马桶垫不掉毛拉链款可爱卡通柔软马桶套");
            goods1.setPicture("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/12/2a77751d75484212bb1b5a50961eaef7_20241012101813A775.jpg");

            Goods goods2 = new Goods(uuid());
            goods2.setId(2L);
            goods2.setGoodsTypeId(goodsTypeId());
            goods2.setName("蝴蝶花毛绒马桶垫冬季新款女生可爱家用加绒加厚柔软全包围坐便垫");
            goods2.setPicture("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/12/609fd27f8ef1447ab1f55a6d49eed4fd_20241012101611A763.jpg");

            Goods goods3 = new Goods(uuid());
            goods3.setId(3L);
            goods3.setGoodsTypeId(goodsTypeId());
            goods3.setName("无绳跳绳健身减肥运动专用重力计数款负重球专业燃脂器");
            goods3.setPicture("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/12/727a24d3e98649e5b4dbbc040dd2cd0b_20241012100945A114.jpg");

            Goods goods4 = new Goods(uuid());
            goods4.setId(4L);
            goods4.setGoodsTypeId(goodsTypeId());
            goods4.setName("HYUNDAI 家用吹风机 风力十足电吹风 YH1800");
            goods4.setPicture("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/11/2c9e73c02a2741179037dacafff54851_20241011162912A826.jpg?imageMogr2/thumbnail/!80p");

            Goods goods5 = new Goods(uuid());
            goods5.setId(5L);
            goods5.setGoodsTypeId(goodsTypeId());
            goods5.setName("雅觅 中国祥瑞什锦荟萃礼盒875g 节日送礼高档礼盒");
            goods5.setPicture("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/11/2d0c3dfc37844a3db2dd7c3b2e2111e3_20241011161639A763.jpg?imageMogr2/thumbnail/!80p");

            ArrayList<Goods> goods = Lists.newArrayList(goods1, goods2, goods3, goods4, goods5);
            dataList.addAll(goods);
        }
    }

    public String uuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    public Integer goodsTypeId() {
        int randomInt = (int) (Math.random() * 10) + 1;
        return randomInt;
    }

    public void initGoodsType() {
        GoodsType goodsType1 = new GoodsType(1, "家用电器");
        GoodsType goodsType2 = new GoodsType(2, "电子图书");
        GoodsType goodsType3 = new GoodsType(3, "水果");
        GoodsType goodsType4 = new GoodsType(4, "运动潮牌");
        GoodsType goodsType5 = new GoodsType(5, "厨具");
        GoodsType goodsType6 = new GoodsType(6, "水杯");
        GoodsType goodsType7 = new GoodsType(7, "耳机");
        GoodsType goodsType8 = new GoodsType(8, "手机");
        GoodsType goodsType9 = new GoodsType(9, "电脑配件");
        GoodsType goodsType10 = new GoodsType(10, "牛奶");
        List<GoodsType> goodsTypes = Lists.newArrayList(goodsType1, goodsType2, goodsType3, goodsType4, goodsType5, goodsType6, goodsType7, goodsType8, goodsType9, goodsType10);
        goodsTypeList.addAll(goodsTypes);
    }

    public List<GoodsType> selectGoodsType(GoodsType param) {
        if (CollectionUtils.isEmpty(goodsTypeList)) {
            initGoodsType();
        }
        String value = param.getName();
        if (value != null && value.length() > 0) {
            List<GoodsType> collect = goodsTypeList.stream().filter(item -> item.getName().contains(value)).toList();
            Integer pageNum = param.getPageNum();
            Integer pageSize = param.getPageSize();
            return getPage(collect, pageNum, pageSize);
        }
        Integer pageNum = param.getPageNum();
        Integer pageSize = param.getPageSize();
        return getPage(goodsTypeList, pageNum, pageSize);
    }

    public GoodsType selectGoodsTypeId(Integer goodsTypeId) {
        return goodsTypeList.stream().filter(item -> item.getId().equals(goodsTypeId)).findFirst().orElse(null);
    }

    public static <T> List<T> getPage(List<T> list, int pageNum, int pageSize) {
        if (list == null || list.isEmpty()) {
            return List.of();
        }

        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, list.size());

        if (fromIndex >= list.size()) {
            return List.of();
        }

        return subList(list, fromIndex, toIndex);
    }

    public static <T> List<T> subList(List<T> list, int fromIndex, int toIndex) {
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }

        if (fromIndex < 0) {
            fromIndex = 0;
        }

        if (toIndex > list.size()) {
            toIndex = list.size();
        }

        if (fromIndex >= toIndex) {
            return new ArrayList<>();
        }

        return new ArrayList<>(list.subList(fromIndex, toIndex));
    }

    public String goodsPic() {
        List<String> stringList = Lists.newArrayListWithCapacity(100);
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/fe691faeb8f048a8a83965069bcaffd7_20241019144555A214.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/24dfac2869ac417696ddccbc4eaf999e_20241019142839A829.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/c8745166341441ceb39dd0340894fcc8_20241019133944A197.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/5bc77e97e331420a8c985b1e900a444b_20241019132531A190.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/57c2179a94874e6ca2f081705277f7d1_20241019111830A720.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/0b398b89c011438d86681046ce0c1414_20241018182554A035.jpg");

        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/4e8862bd41074940a183edf5f61ad932_20241018154719A777.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/def79e03790f4fdda9deb8d9388b067a_20241018160548A814.jpeg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/2aef16637f3a4af49ef38200bd3613d1_20241018161350A460.jpeg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/5e022133addd40b0baa25d115f48a054_20241018161703A843.jpg");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/18/0e6005ff5a6a48c89a5d137bc4f4183a_20241018172018A977.png");
        stringList.add("https://zhongba-1311223706.cos-website.ap-shanghai.myqcloud.com/2024/10/19/f37c3dfabaa941f58ac8b96acaec4779_20241019110128A711.jpg");
        return getRandomElement(stringList);
    }

    public static <T> T getRandomElement(List<T> list) {
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("List must not be null or empty");
        }
        Random random = new Random();
        return list.get(random.nextInt(list.size()));
    }

}
